const { static } = require("express");
module.exports = class User extends require("./model") {
  /**
   *  获取指定用户日记
   *  用户登陆后可方便管理个人日记
   * @param {inteaer} user_id
   *
   */
  static getPersinal(user_id) {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM persinal WHERE user_id = ? ORDER BY TIME DESC";
      this.query(sql, user_id)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取个人日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取全部日记
   *
   */
  static getPersinalList() {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM persinal";
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取热门推荐失败：${err.message}`);
          reject(err);
        });
    });
  }

  // 获取日记详情
  static getDetails(id) {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM persinal WHERE id = ? ORDER BY id DESC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取指定日记详情失败：${err.message}`);
          reject(err);
        });
    });
  }
  // 获取上一篇日记
  static getPrevPersinal(id) {
    return new Promise((resolve, reject) => {
      let sql =
        "SELECT id,title FROM persinal WHERE id < ? ORDER BY id DESC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res[0]);
        })
        .catch((err) => {
          console.log(`获取上一篇日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  // 获取下一篇日记失败
  static getNextPersinal(id) {
    return new Promise((resolve, reject) => {
      let sql =
        "SELECT id,title FROM persinal WHERE id > ? ORDER BY id ASC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res[0]);
        })
        .catch((err) => {
          console.log(`获取下一篇日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  上传日记
   * @param {Object} article 日记对象
   */
  static persinalAdd(article) {
    return new Promise((resolve, reject) => {
      let sql = "INSERT INTO persinal SET ?";
      this.query(sql, article)
        .then((res) => {
          resolve(res.insertId);
        })
        .catch((err) => {
          console.log(`添加日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  // 删除指定用户日记
  static delPersinal(id) {
    return new Promise((resolve, reject) => {
      let sql = "DELETE FROM persinal WHERE id = ?";
      this.query(sql, id)
        .then((res) => {
          resolve(res.affectedRows);
        })
        .catch((err) => {
          console.log(`删除日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  添加日记
   * @param {Object} persinal 文章对象
   */
  static persinalAdd(persinal) {
    return new Promise((resolve, reject) => {
      let sql = "INSERT INTO persinal SET ?";
      this.query(sql, persinal)
        .then((res) => {
          resolve(res.insertId);
        })
        .catch((err) => {
          console.log(`添加日记失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  编辑日记
   * @param {Object} persinal 文章对象
   */
  static persinalEdit(persinal) {
    return new Promise((resolve, reject) => {
      let sql =
        "UPDATE persinal SET title = ?,content = ?,description_image_path = ?,user_id = ?,realname = ?,`time` = ? WHERE id = ?";
      console.log(persinal);
      this.query(sql, [...persinal])
        .then((res) => {
          resolve(res.insertId);
        })
        .catch((err) => {
          console.log(`添加日记失败：${err.message}`);
          reject(err);
        });
    });
  }
};
